class Lottery extends eui.Component implements  eui.UIComponent {
	public constructor() {
		super();
		this.skinName = "lotterySkin";
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}
	private bgg;//动画背景
	private body;//背景最大group
	public beginBtn;//开始按钮
	public begin;//开始的label
	public begin0;
	public begin1;
	private bod;//边框
	private arr = [0,1,2,4,7,6,5,3];//奖品项的下标
	private choujiangkuang;//抽奖框
	private paiImg;
	private jiangpin;
	private canClick=true;
	protected childrenCreated():void
	{
			// console.log(this.jiangpin)
		super.childrenCreated();
		this.playAnimation(this.bgg,true);
		this.body.height = this.stage.stageHeight;
		let arr = this.choujiangkuang.$children;
		
			var sound = new egret.Sound();
			var url = "resource/assets/lottery/9027.wav"
			var souns = new egret.Sound();
			var urls  = "resource/assets/lottery/8804.wav"
		this.beginBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
			
			sound.load(url);
			souns.load(urls);
			if(!this.canClick){
				return ;
			} 
			this.canClick = false;
			this.begin.visible = false;
			this.begin0.visible = true;
			this.begin1.visible = false;
			let timer = new egret.Timer(800,-1);
			// console.log(timer)
			let i  = (Math.random()*8) | 0;
			let oriPos = i;
			let targetPos = 5;
			timer.addEventListener(egret.TimerEvent.TIMER,function(){
				sound.play(0,1);
				if(timer.currentCount<10){
					timer.delay*=.8
				}
				else{
					timer.delay*=1.1
				}
				// console.log(timer.currentCount,7-oriPos+28+targetPos);
				if(timer.currentCount==8-oriPos+8*4+targetPos){
					
					souns.play(0,1)
					timer.stop();
					this.begin1.visible = true;
					this.begin.visible = false;
					this.begin0.visible = false;
					this.canClick = true;
				}

				i++;
				if(i>7) i=0;
				this.chooseR(i); 
			},this)


			timer.start()

		},this)
	}
	//传入下标值，让对应的group改变边框颜色，以及放大
	private chooseR(i){
		// this.count++;
			this.choujiangkuang.$children.forEach(cur=>{
				cur.scaleX = 1;
				cur.scaleY = 1;
				cur.$children[0].fillColor = 0xffffff;
				cur.$children[0].strokeColor = 0x888888;
			})
			let curG = this.choujiangkuang.getChildByName("g"+i);
			this.choujiangkuang.setChildIndex(curG,10000)
			curG.$children[0].fillColor = 0xffffff;
			curG.$children[0].strokeColor = 0xffff00;
			egret.Tween.get(curG).to({scaleX:1.1,scaleY:1.1},300)
			this.paiImg.source=(curG.$children[1]._source)
			this.jiangpin.text = (curG.$children[2].text)
			
			// console.log(this.paiImg)
			// console.log(curG.$children[1]._source)
			// console.log(curG.$children)

		}
	private playAnimation(target:egret.tween.TweenGroup,isLoop:boolean):void{
		if(isLoop){
			for(var key in target.items){
				target.items[key].props = {loop:true};
			}
		}
		target.play();
	}
	
}

















		// this.touchChildren = true;//在游戏项目中，可以一次性的将容器内所有孩子设置可点
		
		//开始按钮
		// this.beginBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
		// 	this.beginBtn.removeChild(this.begin);
		// 	if(this.isclick){
		// 	this.beginInx = Math.floor(Math.random()*this.arr.length);
		// 	// lotteryBox(2);
		// 	// this.isclick = false;
		// 	if(this.i<this.beginInx){
		// 		this.lotteryTimer=setInterval(function(){
		// 			for(this.j=0;this.j<this.arr.length;this.j++);
		// 			this.arr[this.j].removeChild(this.bod);
		// 		},130)
		// 		this.arr[this.i%this.arr.length].addChild(this.bod);
		// 		this.i++;
		// 		if(this.i===this.beginInx){
		// 			clearInterval(this.lotteryTimer);
		// 		}
		// 	}
		// 	}
		// },this)
		// function lotteryBox(index){
		// 	this.count++;
		// 	this.arriveIdx = this.arr[this.beginInx];
		// 	this.choujiangkuang.eq(this.arriveIdx).addChild(this.bod).siblings().removeChild(this.bod);
		// 	if(this.beginInx==7){
		// 		this.beginInx=0
		// 	}else{
		// 		this.beginInx++;
		// 	}
		// 	if(this.count==5||this.count==20){
		// 		this.speed = 500;
		// 	}else{
		// 		this.speed = 200;
		// 	}
		// 	this.lotteryTimer = setTimeout(function(){
		// 		lotteryBox(index)
		// 	},this.speed)
		// }
			// if(this.count>=24&&this.arriveIdx==index)